/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ronsicecream.pos.mk.transaction.receiptproductiondetail;

import com.buanalintas.bynga.Action;
import com.buanalintas.bynga.Field;
import com.buanalintas.bynga.Logger;
import com.buanalintas.bynga.Page;
import com.buanalintas.bynga.Persistence;
import com.buanalintas.bynga.Utility;
import com.ronsicecream.pos.entities.PosTransactionMkReceiptProductionDetail;
import com.ronsicecream.pos.entities.PosTransactionMkWorkOrderDetail;
import java.util.List;
import javax.persistence.EntityManager;

/**
 *
 * @author user
 */
public class ReceiptProductionDetail extends Page {

    private Field receiptDetailId;
    private Field receiptDetailQuantity;
    private Field receiptDetailPrice;
    private Field receiptDetailDescription;
//    private Field receiptDetailSatuanId;
    private Field receiptDetailItemId;
    private Field receiptDetailReceiptId;
    private Field receiptDetailDateExpired;
    private Field receiptDetailDateReceipt;
    private Field receiptDetailBarcode;

    public ReceiptProductionDetail() {
        super();

        setTheClass(PosTransactionMkReceiptProductionDetail.class);




        receiptDetailId = new Field();
        receiptDetailId.setName("receiptDetailId");
        receiptDetailId.setCaption("id");
        //receiptDetailId.setInfo("receipt detail id");
        receiptDetailId.setListProperties(" align=\"left\" width=\"1%\" nowrap");
        receiptDetailId.setInputProperties(" size=\"20\"");
        getFields().add(receiptDetailId);

        receiptDetailBarcode = new Field();
        receiptDetailBarcode.setName("receiptDetailBarcode");
        receiptDetailBarcode.setCaption("barcode");
        receiptDetailBarcode.setListProperties(" align=\"center\" width=\"15%\" nowrap");
        receiptDetailBarcode.setInputProperties(" size=\"10\" style=\"text-align: right;\" readonly");
        receiptDetailBarcode.setInputCase(Field.CASE_UPPER);
        getFields().add(receiptDetailBarcode);

        receiptDetailDateReceipt = new Field();
        receiptDetailDateReceipt.setName("receiptDetailDateReceipt");
        receiptDetailDateReceipt.setCaption("Date Receipt");
        receiptDetailDateReceipt.setRequired(true);
        receiptDetailDateReceipt.setListProperties(" align=\"left\" width=\"10%\" nowrap");
        receiptDetailDateReceipt.setInputProperties(" size=\"20\"");
        receiptDetailDateReceipt.setDefaultValue(Utility.getFirstDate());
        getFields().add(receiptDetailDateReceipt);

        receiptDetailDateExpired = new Field();
        receiptDetailDateExpired.setName("receiptDetailDateExpired");
        receiptDetailDateExpired.setCaption("Date Expired");
        receiptDetailDateExpired.setRequired(true);
        receiptDetailDateExpired.setListProperties(" align=\"left\" width=\"10%\" nowrap");
        receiptDetailDateExpired.setInputProperties(" size=\"20\"");
        receiptDetailDateExpired.setDefaultValue(Utility.getLastDate());
        getFields().add(receiptDetailDateExpired);

        receiptDetailQuantity = new Field();
        receiptDetailQuantity.setName("receiptDetailQuantity");
        receiptDetailQuantity.setCaption("quantity");
        //receiptDetailQuantity.setInfo("receipt detail quantity");
        receiptDetailQuantity.setListProperties(" align=\"left\" width=\"5%\" nowrap");
        receiptDetailQuantity.setInputProperties(" size=\"20\"");
        getFields().add(receiptDetailQuantity);

        receiptDetailPrice = new Field();
        receiptDetailPrice.setName("receiptDetailPrice");
        receiptDetailPrice.setCaption("price");
        //receiptDetailPrice.setInfo("receipt detail price");
        receiptDetailPrice.setListProperties(" align=\"left\" width=\"10%\" nowrap");
        receiptDetailPrice.setInputProperties(" size=\"20\"");
        getFields().add(receiptDetailPrice);

        receiptDetailDescription = new Field();
        receiptDetailDescription.setName("receiptDetailDescription");
        receiptDetailDescription.setCaption("description");
        //receiptDetailDescription.setInfo("receipt detail description");
        receiptDetailDescription.setListProperties(" align=\"left\" width=\"50%\" nowrap");
        receiptDetailDescription.setInputProperties(" cols=\"50\" rows=\"5\"");
        receiptDetailDescription.setInputType(Field.INPUT_TEXT_AREA);
        receiptDetailDescription.setInputCase(Field.CASE_UPPER);
        getFields().add(receiptDetailDescription);

//        receiptDetailSatuanId = new Field();
//        receiptDetailSatuanId.setName("receiptDetailSatuanId");
//        receiptDetailSatuanId.setCaption("satuan");
//        //posMasterSatuan.setInfo("pos master satuan");
//        receiptDetailSatuanId.setListProperties(" align=\"left\" width=\"10%\" nowrap");
//        receiptDetailSatuanId.setInputProperties(" size=\"20\"");        
//        getFields().add(receiptDetailSatuanId);
//        
        receiptDetailItemId = new Field();
        receiptDetailItemId.setName("receiptDetailItemId");
        receiptDetailItemId.setCaption("product");
        //posMasterItem.setInfo("pos master item");
        receiptDetailItemId.setListProperties(" align=\"left\" width=\"15%\" nowrap");
        receiptDetailItemId.setInputProperties(" size=\"20\"");
        receiptDetailItemId.getSelectorColumns().add(new String[]{"itemName", " Nama", "width=\"40%\""});
        receiptDetailItemId.getSelectorColumns().add(new String[]{"itemCode", " Code", "width=\"10%\" nowrap"});
        receiptDetailItemId.getSelectorColumns().add(new String[]{"itemTypeId", " Type", "width=\"20%\""});
        receiptDetailItemId.getSelectorColumns().add(new String[]{"itemMinimum", " Minimum", "width=\"10%\""});
        receiptDetailItemId.getSelectorColumns().add(new String[]{"itemSatuanId", " Satuan", "width=\"10%\""});
        receiptDetailItemId.getSelectorColumns().add(new String[]{"itemRasaId", " Rasa", "width=\"10%\""});
        getFields().add(receiptDetailItemId);

        receiptDetailReceiptId = new Field();
        receiptDetailReceiptId.setName("receiptDetailReceiptId");
        receiptDetailReceiptId.setCaption("receipt");
        //posTransactionRequestFromMainKitchen.setInfo("pos transaction request from main kitchen");
        receiptDetailReceiptId.setListProperties(" align=\"left\" width=\"1%\" nowrap");
        receiptDetailReceiptId.setInputProperties(" size=\"20\"");
        getFields().add(receiptDetailReceiptId);


        fillFields();
        getFieldsAdd().clear();
        getFieldsAdd().add(receiptDetailItemId);
        getFieldsAdd().add(receiptDetailQuantity);
        getFieldsAdd().add(receiptDetailPrice);
        getFieldsAdd().add(receiptDetailDescription);

        getFieldsEdit().clear();
        getFieldsEdit().add(receiptDetailItemId);
        getFieldsEdit().add(receiptDetailQuantity);
        getFieldsEdit().add(receiptDetailDateReceipt);
        getFieldsEdit().add(receiptDetailDateExpired);
        getFieldsEdit().add(receiptDetailPrice);
        getFieldsEdit().add(receiptDetailDescription);

        getFieldsList().clear();
        getFieldsList().add(receiptDetailItemId);
        getFieldsList().add(receiptDetailBarcode);
        getFieldsList().add(receiptDetailQuantity);
        getFieldsList().add(receiptDetailDateReceipt);
        getFieldsList().add(receiptDetailDateExpired);
        getFieldsList().add(receiptDetailPrice);
        getFieldsList().add(receiptDetailDescription);

        getFieldsView().clear();
        getFieldsView().add(receiptDetailItemId);
        getFieldsView().add(receiptDetailBarcode);
        getFieldsView().add(receiptDetailQuantity);
        getFieldsView().add(receiptDetailDateReceipt);
        getFieldsView().add(receiptDetailDateExpired);
        getFieldsView().add(receiptDetailPrice);
        getFieldsView().add(receiptDetailDescription);

        getActions().add(Action.TRANS_DETAIL_LIST);
        getActions().add(Action.TRANS_DETAIL_ADD);
        getActions().add(Action.TRANS_DETAIL_EDIT);
    }

    @Override
    public void loadDetail(List datas, String id) {
        try {
            EntityManager em = getEntityManager();
            List<PosTransactionMkWorkOrderDetail> request = em.createQuery("SELECT t FROM PosTransactionMkWorkOrderDetail t "
                    + "WHERE t.workOrderDetailWorkOrderId.workOrderId=" + id.toString())
                    .setHint(Persistence.REFRESH_HINT, true).getResultList();
            datas.clear();
            for (PosTransactionMkWorkOrderDetail pd : request) {
                PosTransactionMkReceiptProductionDetail rd = new PosTransactionMkReceiptProductionDetail();
                rd.setReceiptDetailItemId(pd.getWorkOrderDetailItemId());
                rd.setReceiptDetailDateReceipt(Utility.getTimestamp());
                rd.setReceiptDetailQuantity(pd.getWorkOrderDetailQuantity());
                rd.setReceiptDetailPrice(pd.getWorkOrderDetailPrice());
                rd.setReceiptDetailDescription(pd.getWorkOrderDetailDescription());
                rd.setReceiptDetailDateReceipt(Utility.getTimestamp());
//                if ((rd.getReceiptDetailQuantity().intValue()) > 0) {
                datas.add(rd);
//                }
            }
        } catch (Exception e) {
            Logger.getInstance().log(e);
            System.out.println("Errrorr " + e);
        }
    }
}
